package com.itheima.controller;

import com.itheima.Service.BannerService;
import com.itheima.pojo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/banners")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    /**
     * 分页查询轮播图列表
     */
    @GetMapping
    public Result page() {
        log.info("查询轮播图");
        List<Banner> pageResult = bannerService.page();
        return Result.success(pageResult);
    }

    /**
     * 获取轮播图详情
     */
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        log.info("查询轮播图详情，ID：{}", id);
        Banner banner = bannerService.get(id);
        return Result.success(banner);
    }

    /**
     * 添加轮播图
     */
    @PostMapping
    public Result add(@RequestBody Banner banner) {
        log.info("添加轮播图：{}", banner);
        bannerService.add(banner);
        return Result.success();
    }

    /**
     * 更新轮播图
     */
    @PutMapping
    public Result update(@RequestBody Banner banner) {
        log.info("更新轮播图信息：{}", banner);
        bannerService.update(banner);
        return Result.success();
    }

    /**
     * 更新轮播图状态
     */
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable Integer id, @RequestBody StatusUpdateParam statusParam) {
        log.info("更新轮播图状态，ID：{}，状态：{}", id, statusParam.getStatus());
        bannerService.updateStatus(id, statusParam.getStatus());
        return Result.success();
    }

    /**
     * 轮播图排序
     */
    /*@PutMapping("/sort")
    public Result sort(@RequestBody SortParam sortParam) {
        log.info("轮播图排序：{}", sortParam.getIds());
        bannerService.sort(sortParam.getIds());
        return Result.success();
    }*/

    /**
     * 删除轮播图
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除轮播图，ID：{}", id);
        bannerService.delete(id);
        return Result.success();
    }

    /**
     * 获取前端展示的轮播图列表
     */
    @GetMapping("/active")
    public Result activeList() {
        log.info("获取前端展示的轮播图列表");
        List<Banner> banners = bannerService.listActive();
        return Result.success(banners);
    }
}
